from .models import Staff
from django.http import HttpResponse
import json
from holidays.views import fuck_cors
from django.core import serializers
from django.template import loader
from django.db.models import ObjectDoesNotExist
# Create your views here.


def test(request):
    if request.method == "OPTIONS":
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        response["Access-Control-Allow-Origin"] = "http://localhost:4200"
        response['Access-Control-Allow-Credentials'] = 'true'
        response["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS"
        response["Access-Control-Allow-Headers"] = "Origin,Cookie,Accept,Token,Access-Control-Allow-Origin," \
                                                   "Access-Control-Allow-Headers,X-Requested-With,Content-Type," \
                                                   "Access-Control-Allow-Credentials, set-cookie"
        return response
    request.session['username'] = '1001'
    response = HttpResponse(content=json.dumps({"msg": "test"}), content_type="application/json", status=200)
    response["Access-Control-Allow-Origin"] = "http://localhost:4200"
    response['Access-Control-Allow-Credentials'] = 'true'
    response["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS"
    response["Access-Control-Allow-Headers"] = "Origin,Cookie,Accept,Token,Access-Control-Allow-Origin," \
                                               "Access-Control-Allow-Headers,X-Requested-With,Content-Type," \
                                               "Access-Control-Allow-Credentials, set-cookie"
    return response


def login(request):
    if request.method == "OPTIONS":
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        response["Access-Control-Allow-Origin"] = "http://localhost:4200"
        response['Access-Control-Allow-Credentials'] = 'true'
        response["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS,PATCH"
        response["Access-Control-Allow-Headers"] = "Origin,Cookie,Accept,Token,Access-Control-Allow-Origin," \
                                                   "Access-Control-Allow-Headers,X-Requested-With,Content-Type," \
                                                   "Access-Control-Allow-Credentials, set-cookie"
        return response
    print(request.body)
    req = json.loads(request.body)
    try:
        staff = Staff.objects.get(username=req['username'], password=req['password'])
    except ObjectDoesNotExist:
        res = {"msg": "密码错误"}
        return HttpResponse(content=json.dumps(res,  ensure_ascii=False), content_type='application/json', status=404)
    request.session['username'] = staff.username
    response = HttpResponse(content=staff.to_json(), content_type='application/json', status=200)
    response["Access-Control-Allow-Origin"] = "http://localhost:4200"
    response['Access-Control-Allow-Credentials'] = 'true'
    response["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS,PATCH"
    response["Access-Control-Allow-Headers"] = "Origin,Cookie,Accept,Token,Access-Control-Allow-Origin," \
                                               "Access-Control-Allow-Headers,X-Requested-With,Content-Type," \
                                               "Access-Control-Allow-Credentials, set-cookie"
    return response


def change_password(request):
    if request.method == "OPTIONS":
        return HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
    res = {}
    req = json.loads(request.body)
    try:
        staff = Staff.objects.get(username=req['username'], password=req['oldPassword'])
    except ObjectDoesNotExist:
        res['msg'] = '原密码错误'
        return HttpResponse(content=json.dumps(res, ensure_ascii=False), content_type='application/json', status=404)
    staff.password = req['newPassword']
    staff.save()
    res['msg'] = '修改成功'
    return HttpResponse(content=json.dumps(res, ensure_ascii=False), content_type='application/json', status=200)


'''
def register(request):
    if request.method == "OPTIONS":
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        response["Access-Control-Allow-Origin"] = "http://localhost:4200"
        response['Access-Control-Allow-Credentials'] = 'true'
        response["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS,PATCH"
        response["Access-Control-Allow-Headers"] = "Origin,Cookie,Accept,Token,Access-Control-Allow-Origin," \
                                                   "Access-Control-Allow-Headers,X-Requested-With,Content-Type," \
                                                   "Access-Control-Allow-Credentials, set-cookie"
        return response
    req = json.loads(request.body)

    Staff.objects.create(**req)
'''


def departments(request):
    if request.method == 'OPTIONS':
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        fuck_cors(response)
        return response
    staffs = Staff.objects.all()
    staffs = serializers.serialize("json", staffs)
    response = HttpResponse(content=staffs, content_type='application/json', status=200)
    fuck_cors(response)
    return response


def modify_holidays(request):
    if request.method == 'OPTIONS':
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        fuck_cors(response)
        return response
    req = json.loads(request.body)
    print(req['annualLeave'])
    staff = Staff.objects.get(username=req['username'])
    staff.annualLeave = req['annualLeave']

    staff.save()
    response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
    fuck_cors(response)
    return response


def register(request):
    if request.method == 'OPTIONS':
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        fuck_cors(response)
        return response
    req = json.loads(request.body)
    print(req)
    num = Staff.objects.filter(username=req['username']).count()
    if num == 0:
        Staff.objects.create(**req)
        response = HttpResponse(content=json.dumps({}), content_type='application/json', status=200)
        fuck_cors(response)
        return response
    else:
        res = {"msg": "用户名已存在"}
        response = HttpResponse(json.dumps(res, ensure_ascii=False), content_type='application/json', status=200)
        fuck_cors(response)
        return response

